/**
 * 
 */
package com.nh.actionPhone;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.nh.entity.NgExpertInterlocution;

/**
 * 项目名称：NotoginsengManage
 * 
 * 类名称：UserUploadAction
 * 
 * 类描述： 获取Android端上传过来的信息
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-11-11 下午09:19:03
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-11-11 下午09:19:03
 * 
 * 修改备注：
 * 
 * @version
 */
public class UserUploadAction extends BasePhoneAction {
	// 上传文件域
	private File image;
	// 上传文件类型
	private String imageContentType;
	// 封装上传文件名
	private String imageFileName;

	private Long optionId;

	@Override
	public String execute() {
		HttpServletRequest request = ServletActionContext.getRequest();
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try {
			System.out.println("获取Android端传过来的普通信息：");
			System.out.println("用户名：" + request.getParameter("username"));
			System.out.println("密码：" + request.getParameter("pwd"));
			System.out.println("年龄：" + request.getParameter("age"));
			System.out.println("文件名：" + request.getParameter("fileName"));
			System.out.println("获取Android端传过来的文件信息：");
			System.out.println("文件存放目录: " + getSavePath());
			System.out.println("文件名称: " + imageFileName);
			System.out.println("文件大小: " + image.length());
			System.out.println("文件类型: " + imageContentType);

			// 获得图片后缀名
			String fileExt = getImageFileName().substring(getImageFileName().lastIndexOf(".")).toLowerCase();
			String imagePath = "attached/user_image/" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/";
			String newImgName = new Date().getTime() + fileExt;
			fos = new FileOutputStream(getSavePath() + imagePath + "/" + newImgName);
			fis = new FileInputStream(getImage());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) != -1) {
				fos.write(buffer, 0, len);
			}
			writeImage(imagePath + "/" + newImgName);
			System.out.println("文件上传成功");
		} catch (Exception e) {
			System.out.println("文件上传失败");
			e.printStackTrace();
		} finally {
			close(fos, fis);
		}
		return SUCCESS;
	}

	private void writeImage(String imagePath) {
		imagePath = this.getProjectURL() + imagePath;
		NgExpertInterlocution expertInterlocution = this.getBaseService().findById(optionId, NgExpertInterlocution.class);
		String imag = "<br/><img src='" + imagePath + "' width='100%' height='100%' />";
		expertInterlocution.setQuestion(expertInterlocution.getQuestion() + imag);
	}

	/**
	 * @return the optionId
	 */
	public Long getOptionId() {
		return optionId;
	}

	/**
	 * @param optionId
	 *            the optionId to set
	 */
	public void setOptionId(Long optionId) {
		this.optionId = optionId;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	private void close(FileOutputStream fos, FileInputStream fis) {
		if (fis != null) {
			try {
				fis.close();
				fis = null;
			} catch (IOException e) {
				System.out.println("FileInputStream关闭失败");
				e.printStackTrace();
			}
		}
		if (fos != null) {
			try {
				fos.close();
				fis = null;
			} catch (IOException e) {
				System.out.println("FileOutputStream关闭失败");
				e.printStackTrace();
			}
		}
	}

}
